using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.DataAccess;
namespace IGlobalist.Cms.Bll
{
    public class ContentController : BaseController
    {
        public static IContent ConvertCmsObject2Content(ICmsObject obj)
        {
            IContent content = new Content();

            content.FromCmsObject(obj);
            if (obj==null) return null;
            try
            {
                IUser u = GetNameByAuthor(content.Author);
                content.AuthorName = u.Name;
                content.AuthorInfo = "Joined: " + u.CreatedDate + "<br> Location: " + u.Country;
            }
            catch
            {
            }
            content.NoteList = ContentNodeController.GetContentNoteListLelateToObjectID(obj.ID);
            return content;
        }


        private static IList<IContent> ConvertListObject2Contents(IList<ICmsObject> list)
        {
            IList<IContent> contents = new List<IContent>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                if (obj.ClassTypeID == (short)CmsObjectType.Content)
                {
                    contents.Add(ConvertCmsObject2Content(obj));
                }
            }

        return contents;
        }

        public static ContentList ConvertListObject2ContentList(IList<ICmsObject> list)
        {
            ContentList contents= new ContentList();
            if (list!=null)
                foreach (ICmsObject content in list)
                {
                    if (content.ClassTypeID == (short)CmsObjectType.Content)
                    {
                        contents.Add(ConvertCmsObject2Content(content));
                    }
                }
            return contents;
        }




        public static IContent GetContent(long id)
        {
            return ConvertCmsObject2Content(_Ds.GetObject(id));
        }

   

        public static IList<IContent> GetContentList(long classID)
        {
            return ConvertListObject2Contents(_Ds.GetObjectListByClassID(classID));
        }


        

        public static ContentList GetContentListLelateToObjectID(long objectID, CmsObjectType objectType)
        {
            return ConvertListObject2ContentList(_Ds.GetObjectListRelateToObjectID(objectID, (short) objectType));
        }


        public static ContentList GetContentListAvailable()
        {
           

            return ConvertListObject2ContentList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Content));
        }

        public static ContentList GetContentListBySearch(string search)
        {
            return ConvertListObject2ContentList(_Ds.GetObjectListAllBySearch(search));
        }

        public static long InsertContent(IContent content)
        {
            long id = _Ds.InsertObject((ICmsObject)content);
            if (content.PageID > 0)
            {
                _Ds.InsertObjectRelation(content.PageID, id, (short) CmsObjectType.Page);
            }

            if (content.CategoryID > 0)
            {
                _Ds.InsertObjectRelation(content.CategoryID, id, (short)CmsObjectType.ContentCategory);
            }

            
                InsertUpdateDeleleContentNode(id, content.NoteList);
            

          
            return id;
        }

        private static void InsertUpdateDeleleContentNode(long contentID, ContentNoteList nodeList)
        {
            if (nodeList != null && nodeList.Count > 0)
            {
                foreach (IContentNote contentNode in nodeList)
                {
                    switch (contentNode.ActionType)
                    {
                        case ActionType.Add:
                            contentNode.ParentID = contentID;
                            long noteID=ContentNodeController.InsertContentNote(contentNode);
                            //_Ds.InsertObjectRelation(contentID, noteID, (short)CmsObjectType.Content);
                            break;
                        case ActionType.Edit:
                            ContentNodeController.UpdateContentNote(contentNode);
                            break;
                        case ActionType.Delete:
                            ContentNodeController.DeleteContentNote(contentNode.ID);
                            break;
                    }
                }
            }
        }

        public static void UpdateContent(IContent content)
        {

            IContent contentOld = GetContent(content.ID);
            _Ds.UpdateObject((ICmsObject)content);

            
            if (contentOld.PageID != content.PageID)
            {
                _Ds.DeleteObjectOneRelation(contentOld.PageID, content.ID);
                if (content.PageID > 0)
                {
                    _Ds.InsertObjectRelation(content.PageID, content.ID, (short)CmsObjectType.Page);
                }
            }

            if (contentOld.CategoryID != content.CategoryID)
            {
                _Ds.DeleteObjectOneRelation(contentOld.CategoryID, content.ID);
                if (content.CategoryID > 0)
                {
                    _Ds.InsertObjectRelation(content.CategoryID, content.ID, (short)CmsObjectType.ContentCategory);
                }
            }

            InsertUpdateDeleleContentNode(content.ID, content.NoteList);
                      
            _Ds.ClearCacheData((short) CmsObjectType.Page);
            
        }

        public static void DeleteContent(long id)
        {
            _Ds.DeleteObject(id);
        }

        private static IUser GetNameByAuthor(long author)
        {
            IUser u = new User();
            u = UserController.GetUser(author);
            return u;
        }
    }
}
